package com.wbsxch.demo04.refactoring;

import java.util.Enumeration;
import java.util.Vector;

import static com.wbsxch.demo04.refactoring.Movie.*;

public class Customer {
    private String _name;

    private Vector _rentals = new Vector();

    public Customer(String _name) {
        this._name = _name;
    }

    public void addRental(Rental arg){
        _rentals.addElement(arg);
    }

    public String getName() {
        return _name;
    }

    public String statement(){
        Enumeration rentals = _rentals.elements();
        String result = "Rental Record for " + getName() + "\n";
        while (rentals.hasMoreElements()){

            Rental each = (Rental) rentals.nextElement();

            result += "\t" + each.getMovie().getTitle() + "\t" + each.getAmount() + "\n";
        }

        result += "Amount owed is " + getTotalAmount() + "\n";
        result += "You earned "+ getFrequentRenterPoints() + " frequent renter points";

        return result;
    }


    public double getTotalAmount(){
        double result = 0;
        Enumeration elements = _rentals.elements();
        while (elements.hasMoreElements()){
            Rental rental = (Rental) elements.nextElement();
            result += rental.getAmount();
        }

        return result;
    }

    public int getFrequentRenterPoints(){
        int result = 0;
        Enumeration elements = _rentals.elements();
        while (elements.hasMoreElements()){
            Rental rental = (Rental) elements.nextElement();
            result += rental.getFrequentRenterPoints();
        }
        return result;
    }


    public static void main(String[] args) {
        method01();
        method02();
    }

    public static void method01(){
        Customer ez = new Customer("EZ");
        Movie movie1 = new Movie("旺角卡门", REGULAR);
        Rental rental1 = new Rental(movie1,5);
        ez.addRental(rental1);

        Movie movie2 = new Movie("冰血暴", NEW_RELEASE);
        Rental rental2 = new Rental(movie2,7);
        ez.addRental(rental2);

        String statement = ez.statement();
        System.out.println(statement.equals("Rental Record for EZ\n" +
                "\t旺角卡门\t6.5\n" +
                "\t冰血暴\t21.0\n" +
                "Amount owed is 27.5\n" +
                "You earned 3 frequent renter points"));
    }

    public static void method02(){
        Customer vn = new Customer("VN");
        Movie movie1 = new Movie("西部世界", CHILDRENS);
        Rental rental1 = new Rental(movie1,3);
        vn.addRental(rental1);

        Movie movie2 = new Movie("权力游戏", CHILDRENS);
        Rental rental2 = new Rental(movie2,5);
        vn.addRental(rental2);

        String statement = vn.statement();
        System.out.println(statement.equals("Rental Record for VN\n" +
                "\t西部世界\t1.5\n" +
                "\t权力游戏\t4.5\n" +
                "Amount owed is 6.0\n" +
                "You earned 2 frequent renter points"));
    }
}
